package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.MenuService;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/menu")
public class TreeMenuController {
    @Reference
    private MenuService menuService;

    /*查询树形控件数据*/
    @RequestMapping("/getAll")
    public Result getAll() {
        List<Menu> menuList = menuService.getAll();
        return Result.success("", menuList);
    }

    /*添加目录*/
    @RequestMapping("/add")
    public Result add(@RequestBody HashMap map) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        menuService.add(map,username);
        return Result.success("");
    }

    //删除菜单
    @RequestMapping("/delete")
    public Result delete(Integer id) {


        return  menuService.delete(id);
    }

    //编辑界面数据回显
    @RequestMapping("/findMenu")
    public Result findMenu(Integer id) {
        Menu menus = menuService.findMenu(id);
        return Result.success("", menus);
    }
    //编辑菜单
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
         menuService.edit(menu);
        return Result.success("编辑成功");
    }
    //动态菜单
    @RequestMapping("/findUserMenu")
    public Result findUserMenu() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Menu> menu= menuService.findUserMenu(user.getUsername());
        return Result.success("",menu);
    }
}
